<template>
	<navigatorComp title="积分明细" bgcColor="none" fontColor="#0E0E0E"></navigatorComp>

	<view class="content" style="overflow: hidden;">
		<view class="coinDetail_card">
			<image src="http://112.124.50.179:8888/imgs/mine/gift.png" mode=""></image>
			<view class="coinDetail_card_label">可用积分</view>
			<view class="coinDetail_card_coin">{{ points }}</view>
		</view>
		<view class="display_flex" style="margin: 40rpx 0 24rpx">
			<up-tabs
				:current="currentTab"
				lineWidth="48rpx"
				lineHeight="5rpx"
				:activeStyle="{ color: '#333333' }"
				:inactiveStyle="{ color: '#999999' }"
				itemStyle="font-weight: 500;font-size: 28rpx;margin-bottom:20rpx;width:250rpx;"
				lineColor="#8E775B"
				:list="tablist"
				@click="changeTab"
			></up-tabs>
		</view>
		<mescroll-uni
			:height="height + 'px'"
			:up="upOption"
			@init="mescrollInit"
			@down="myDownCallback"
			@up="upCallback"
		>
			<view class="recordList">
				<view class="recordItem" v-for="(item, index) in list">
					<view>
						<view class="record_label">{{ item.title }}</view>
						<view v-if="item.remark" class="record_name singleLineOverflowHidden">
							{{item.remark}}
						</view>
						<view class="record_time">{{ formatTimestamp2(item.timestamp) }}</view>
					</view>
					<view class="record_price" v-if="currentTab === 0">+{{ item.number }}</view>
					<view class="record_price record_price_reduce" v-else>-{{ item.number }}</view>
				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref, reactive } from 'vue';
import navigatorComp from '/components/navigator/index.vue';
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js';
import { formatTimestamp2 } from '@/utils/time.js';
import store from '@/store/index.js';
import MineApi from '@/API/mine';

const mineApi = new MineApi();
const points = ref(0); //积分
const height = ref(500);
const { mescrollInit, downCallback, getMescroll } = useMescroll(); // 调用mescroll的hook
const upOption = {
	page: {
		size: 10 // 每页数据的数量
	},
	noMoreSize: 4 //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
	// empty: {
	// 	use: false //不显示空图
	// }
};
const lastId = ref(0); //最后一个元素的id
const tablist = reactive([
	{
		name: '获取记录'
	},
	{
		name: '消耗记录'
	}
]);
const currentTab = ref(0); //默认为获取记录，0
const list = ref([]);

onLoad(() => {
	// #ifdef MP-WEIXIN
	height.value =
		uni.getSystemInfoSync().windowHeight -
		getApp().globalData.bottom -
		getApp().globalData.navHeight -
		uni.upx2px(400);
	// #endif

	// #ifdef APP || H5
	height.value =
		uni.getSystemInfoSync().windowHeight -
		getApp().globalData.bottom -
		getApp().globalData.navHeight -
		uni.upx2px(88) -
		uni.upx2px(400);
	// #endif

	//积分
	points.value = store.state.userInfo.userInfo.bonus;
});

const changeTab = (e) => {
	currentTab.value = e.index;
	myDownCallback();
};

/**
 * 上拉加载
 */
const upCallback = (mescroll) => {
	mineApi
		.getBonusRecord({
			getType: currentTab.value + 1, //1:充值，2:消耗
			lastId: lastId.value
		})
		.then((res) => {
			const curPageData = res.data.items || []; // 当前页数据
			console.log(curPageData);
			if (lastId.value === 0) list.value = []; //如果是第一页需手动制空列表
			list.value = list.value.concat(curPageData); //追加新数据\
			if (curPageData.length > 0) {
				lastId.value = curPageData[curPageData.length - 1].id;
			}
			mescroll.endSuccess(curPageData.length); // 请求成功, 结束加载
		})
		.catch(() => {
			mescroll.endErr(); // 请求失败, 结束加载
		});
	mescroll.endErr();
};

const myDownCallback = (mescroll) => {
	lastId.value = 0;
	downCallback();
};
</script>

<style lang="scss">
page {
	background: linear-gradient(164deg, #dfeaff 0%, #f7f8fa 50%, #f7f8fa 100%);
	/* #ifdef H5 */
	padding-top: 0;
	/* #endif */
}

.coinDetail_card {
	margin-top: 24rpx;
	width: 100%;
	height: 244rpx;
	border-radius: 16rpx;
	background: linear-gradient(120deg, rgba(136, 176, 254, 0.8) 0%, #e8f0ff 100%);
	position: relative;
	display: flex;
	flex-direction: column;
	justify-content: center;

	image {
		width: 220rpx;
		height: 220rpx;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.coinDetail_card_label {
		font-weight: 500;
		font-size: 28rpx;
		color: #0e0e0e;
		line-height: 40rpx;
		margin-left: 56rpx;
	}

	.coinDetail_card_coin {
		font-weight: 600;
		font-size: 48rpx;
		color: #0e0e0e;
		line-height: 68rpx;
		margin-top: 16rpx;
		margin-left: 56rpx;
	}
}

.recordList {
	padding-bottom: 32rpx;

	.recordItem {
		margin-bottom: 24rpx;
		width: 100%;
		background: #ffffff;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 32rpx 24rpx 32rpx 32rpx;
		box-sizing: border-box;

		.record_label {
			font-weight: 400;
			font-size: 32rpx;
			color: #333333;
			line-height: 38rpx;
		}

		.record_name {
			width: 432rpx;
			height: 34rpx;
			font-weight: 400;
			font-size: 24rpx;
			color: #333333;
			line-height: 34rpx;
			margin-top: 12rpx;
		}

		.record_time {
			font-weight: 400;
			font-size: 24rpx;
			color: #c5c5c5;
			line-height: 34rpx;
			margin-top: 12rpx;
		}

		.record_price {
			font-weight: 500;
			font-size: 32rpx;
			color: #ff4c5a;
			line-height: 38rpx;
		}

		.record_price_reduce {
			color: #5bd465;
		}
	}
}
</style>
